#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

void try(int status, const char *message){
	if (status == -1) {
		perror(message);
		exit(EXIT_FAILURE);
	}
}

int main(int argc, char *argv[]) {
	
	if (argc < 3) {
		perror("lack of arguments in diff module");
		exit(EXIT_FAILURE);
	}

	char *exec_argv[] = { "diff" , argv[1], argv[2], NULL };
	int pipefd[2];
	try(pipe(pipefd), "pipefd");
	try(dup2(pipefd[0], 0), "dup2");	//main process will read strongly from the pipe

	pid_t cpid = fork();
	if (cpid == 0) {	//child body
		close(pipefd[0]);	//child cannot read from the pipe(for security)
		try(dup2(pipefd[1], 1), "child dup2");
		try(execvp(exec_argv[0], exec_argv), "execvp");
		close(pipefd[1]);
		exit(EXIT_SUCCESS);
	} else {
		close(pipefd[1]);	//parent cannot write to pipe(for security)
		char buf;
		if (read(0, &buf, 1) > 0) 
			printf("Failed\n");
		else 
			printf("Successful\n");
		close(pipefd[0]);
	}
	return 0;
}
